package com.aisino.inveli.vinda.utils;

import java.math.BigDecimal;

public class MathUtil {
    public static float getTwoPoint(String numStr) {
        BigDecimal b = new BigDecimal(numStr);
        //保留2位小数
        float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        return f1;
    }

    public static float getTwoPoint(Object numStr) {
        float f = Float.parseFloat(numStr.toString());
        BigDecimal b = new BigDecimal(f);
        //保留2位小数
        float f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        return f1;
    }

    public static Double getTwoPointDouble(Object numStr) {
        Double f = Double.parseDouble(numStr.toString());
        BigDecimal b = new BigDecimal(f);
        //保留2位小数
        Double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    /**
     * 保留2位小数
     *
     * @param numStr 商品金额
     * @return
     */
    public static BigDecimal getTwoPointBig(BigDecimal numStr) {
        BigDecimal b = numStr;
        //保留2位小数
        BigDecimal f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP);
        return f1;
    }

    public static float getFourPoint(Object numStr) {
        float f = Float.parseFloat(numStr.toString());
        BigDecimal b = new BigDecimal(f);
        //保留2位小数
        float f1 = b.setScale(4, BigDecimal.ROUND_HALF_UP).floatValue();
        return f1;
    }

    /**
     * 判断str能否转换成字符串
     *
     * @param str
     * @return
     */
    public static boolean isNum(String str) {
        try {

            Float.parseFloat(str);
            return true;
        } catch (Exception e) {

        }
        return false;
    }

    public static String forMatNum(Object num) {
        float f = 0.0f;
        if (num instanceof String) {
            f = Float.valueOf(num.toString());
            return String.format("%.2f", f);
        } else {

            return String.format("%.2f", num);
        }
    }

    public static String add(Object ob1, Object ob2) {
        BigDecimal b1 = new BigDecimal(ob1.toString());
        BigDecimal b2 = new BigDecimal(ob2.toString());
        return b1.add(b2).toString();
    }

    public static String sub(Object ob1, Object ob2) {
        BigDecimal b1 = new BigDecimal(ob1.toString());
        BigDecimal b2 = new BigDecimal(ob2.toString());
        return b1.subtract(b2).toString();
    }

    public static String mul(Object ob1, Object ob2) {
        BigDecimal b1 = new BigDecimal(ob1.toString());
        BigDecimal b2 = new BigDecimal(ob2.toString());
        return b1.multiply(b2).toString();
    }

    public static String div(Object ob1, Object ob2) {
        BigDecimal b1 = new BigDecimal(ob1.toString());
        BigDecimal b2 = new BigDecimal(ob2.toString());
        return b1.divide(b2).toString();
    }
}
